<template>
	<view>
		<view class="tui-header-bg">
			
		</view>
		<view class="tui-mybg-box">
			<view class="tui-header-center" v-if="vuex_token">
				<image :src="vuex_user.user_avatar || emptyAvatar" class="tui-avatar" @click="gotoProfile"></image>
				<view class="tui-info" >
					<view class="tui-nickname">
						{{vuex_user.user_nickname}}
					</view>
					<view class="header-btn-group">
					</view>
				</view>
			</view>
			<view class="tui-header-center" v-else>
				<image :src="emptyAvatar" class="tui-avatar"></image>
				<view class="tui-login" @tap="login">
					<text>登录/注册</text>
					<u-icon name="arrow-right" color="#999" :size="24" unit="rpx"></u-icon>
				</view>
			</view>
			<view class="tui-header-btm">
				<view class="tui-btm-item" v-if="vuex_shop&&vuex_shop.status == 1" @click="gotoPage('/pageA/Store/index')">
					<view class="tui-btm-num">{{ vuex_shop.money || '0' }}</view>
					<view class="tui-btm-text">商家余额</view>
				</view>
				<view class="tui-btm-item" v-if="isExpert" @click="gotoPage('/pages/recommend/index')">
					<view class="tui-btm-num">{{ vuex_user.userMoney || '0' }}</view>
					<view class="tui-btm-text">达人佣金</view>
				</view>
				<view class="tui-btm-item" @click="gotoPage('./myquan?type=1')">
					<view class="tui-btm-num">{{zfj}}</view>
					<view class="tui-btm-text">支付金</view>
				</view>
				<view class="tui-btm-item" @click="gotoPage('./myquan?type=0')" >
					<view class="tui-btm-num">{{gwq}}</view>
					<view class="tui-btm-text">购物券</view>
				</view>
			</view>
		</view>
		<view class="tui-content-box">
			<swiper class="tui-swiper">
				<swiper-item class="tui-swiper-item" v-for="(item,index) in bannerList" @click="gotoPage(item.url)" :key="index">
					<image :src="item.image" class="tui-swiper-img"></image>
				</swiper-item>
			</swiper>
			<view class="tui-box tui-tool-box">
				<view class="tui-cell-header">
					<view class="tui-cell-title">全部订单</view>
					<view class="tui-cell-sub" @click="gotoPage(`/pages/shop/exchange?tabIndex=${0}`)">
						查看
						<u-icon name="arrow-right" color="#999" :size="24" unit="rpx"></u-icon>
					</view>
				</view>
				<view class="tui-order-list">
					<block v-for="(item,index) in dhOrderBtns" :key="index">
						<view class="tui-order-item" @tap="gotoPage(item.pagePath)" >
							<view class="tui-icon-box">
								<image :src="item.iconUrl" class="tui-order-icon"></image>
								<view class="tui-badge tui-badge-red" v-if="item.num>0">{{item.num}}</view>
							</view>
							<view class="tui-order-text">{{item.title}}</view>
						</view>
					</block>
				</view>
			</view>
			<view class="tui-box tui-tool-box">
				<view class="tui-cell-header">
					<view class="tui-cell-title">商家工具</view>
				</view>
				<view class="tui-order-list tui-flex-wrap">
					
					<block v-for="(item,index) in toolsBtns" :key="index" v-if="index>=2">
						<view class="tui-tool-item" @tap="gotoPage(item.pagePath, item.isNeedLogin)" v-if="item.type=='gotoPage'">
							<view class="tui-icon-box">
								<image :src="item.iconUrl" class="tui-tool-icon"></image>
							</view>
							<view class="tui-tool-text">{{item.title}}</view>
						</view>
						<view class="tui-tool-item" @click="handleClick(item.method)" v-if="item.type=='gotoMethod'">
							<view class="tui-icon-box">
								<image :src="item.iconUrl" class="tui-tool-icon"></image>
							</view>
							<view class="tui-tool-text">{{item.title}}</view>
						</view>
					</block>
					
					<!-- <view class="tui-tool-item">
						<button class="contact-btn" @click="showSubscribeModal">
							<view class="tui-icon-box">
								<image src="/static/images/my/icon_service.png" class="tui-tool-icon"></image>
							</view>
							<view class="tui-tool-text">允许消息通知</view>
						</button>
					</view> -->
				</view>
			</view>
			<view class="tui-box tui-tool-box">
				<view class="tui-cell-header">
					<view class="tui-cell-title">常用工具</view>
				</view>
				<view class="tui-order-list tui-flex-wrap">
					<block v-for="(item,index) in toolsBtns" :key="index" v-if="index<2">
						<view class="tui-tool-item" @tap="gotoPage(item.pagePath, item.isNeedLogin)" v-if="item.type=='gotoPage'">
							<view class="tui-icon-box">
								<image :src="item.iconUrl" class="tui-tool-icon"></image>
							</view>
							<view class="tui-tool-text">{{item.title}}</view>
						</view>
						<view class="tui-tool-item" @click="handleClick(item.method)" v-if="item.type=='gotoMethod'">
							<view class="tui-icon-box">
								<image :src="item.iconUrl" class="tui-tool-icon"></image>
							</view>
							<view class="tui-tool-text">{{item.title}}</view>
						</view>
					</block>
				</view>
			</view>
			
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isExpert: false,
				url: 'https://admin.cqycgyl.com',
				bannerList: [],
				emptyAvatar: 'https://admin.cqycgyl.com/uploads/20250915/3a4478cfb58ab6aa53f9233660b76e05.png',
				dhOrderBtns:[
					// {
					// 	title:'待付款',
					// 	iconUrl:'/static/images/my/dfk.png',
					// 	type:'gotoPage',
					// 	pagePath:`/pages/shop/exchange?tabIndex=${1}`,
					// 	num:''
					// },
					// {
					// 	title:'待发货',
					// 	iconUrl:'/static/images/my/dfh.png',
					// 	type:'gotoPage',
					// 	pagePath:`/pages/shop/exchange?tabIndex=${2}`,
					// 	num:''
					// },
					// {
					// 	title:'待收货',
					// 	iconUrl:'/static/images/my/dsh.png',
					// 	type:'gotoPage',
					// 	pagePath:`/pages/shop/exchange?tabIndex=${3}`,
					// 	num:''
					// },
					// {
					// 	title:'待评价',
					// 	iconUrl:'/static/images/my/dpj.png',
					// 	type:'gotoPage',
					// 	pagePath:`/pages/shop/exchange?tabIndex=${4}`,
					// 	num:''
					// }
				],
				toolsBtns: [],
				gwq:0,
				zfj: 0,
				BarTop:'',
				BarHeight:'',
				subscribeStatus: false, // 订阅消息状态
			};
		},
		onLoad() {
			// #ifdef MP
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			this.BarTop = menuButtonInfo.top;
			this.BarHeight = menuButtonInfo.height;
			// #endif
		},
		onShow() {
			this.$u.api.user.getUserData().then(res=>{
				let data = res.data
				this.dhOrderBtns = data.dhOrderBtns
				this.toolsBtns = data.toolsBtns
				this.gwq = data.gwq
				this.zfj = data.zfj
				this.getBannerList();
				this.$u.vuex('vuex_user.userMoney', data.yue);
			});
			if(this.vuex_token){
				this.$u.api.shop.getStorebili().then(res =>{
					if(res.code == 200){
						this.$u.vuex('vuex_shop',res?.data)
						
					}
				})
			}

			
			
		},
		methods: {
			isExpert(){
				this.$u.api.recommend.isExpert().then(res =>{
					if(res.code == 1){
						this.isExpert = true
					}else{
						this.isExpert = false
					}
				})
			},
			getBannerList(){
				this.$u.api.index.getBanner().then(res=>{
					if(res.code == 1){
						this.bannerList = res.data.filter(item=>{
							return item.type == 4
						})
						
						this.bannerList.forEach(item=>{
							item.image = this.url + item.image
						})
					}
				})
			},
			gotoPage(url, isNeedLogin = true){
				if(isNeedLogin){
					if(!this.vuex_token){
						uni.navigateTo({
							url: '/pageA/common/login'
						})
						return
					}
				}
				uni.navigateTo({
					url: url
				})
			},
			handleClick(method) {
				const methodName = method; // 方法名作为变量 
				if (typeof this[methodName] === 'function') {
					this[methodName](); // 动态执行 
				}
			},
			login() {
				uni.navigateTo({
					url: '/pageA/common/login'
				})
			},
			storeCheckIn(){
				if(!this.vuex_token){
					uni.navigateTo({
						url: '/pageA/common/login'
					})
					return
				}
				switch(this.vuex_shop.status){
					case 0:
						uni.navigateTo({
							url:`./outcome?id=${0}`
						})
						break;
					case 1:
						uni.navigateTo({
							url:`./outcome?id=${1}`
						})
					break;
					case 2:
					    uni.navigateTo({
					    	url:`./outcome?id=${2}`
					    })
						break;
					default: 
						uni.navigateTo({
							url: '/pages/recommend/checkCode'
						})
				}
				
			},
			shop(){
				if(!this.vuex_token){
					uni.navigateTo({
						url: '/pageA/common/login'
					})
					return
				}
				
				let status = this.vuex_shop.status
				if(status == 1){
					uni.navigateTo({
						url:'../../pageA/Store/index'
					})
				}else if(status == 0){
					uni.showToast({
						title:"资料审核中...",
						icon:"none"
					})
				}else{
					uni.showToast({
						title:"请先提交商家入驻！",
						icon:"none"
					})
				}
			},
			gotoProfile() {
				if(!this.vuex_token){
					console.log('没有登录')
					uni.navigateTo({
						url: '/pageA/common/login'
					})
					return
				}
				console.log('登录了')
				uni.navigateTo({
					url: '/pages/my/profile'
				})
			},
			recommendExpert(){
				uni.navigateTo({
					url: '/pages/recommend/introduce'
				})
			},
			recommendExpertCenter(){
				uni.navigateTo({
					url: '/pages/recommend/index'
				})
			},
			showSubscribeModal() {
				uni.requestSubscribeMessage({
					tmplIds: ['vCzeNc73dhtcBYw7sg4GoFRzNLIBHaQoLKDgDgP9nQA'], // 替换为实际的模板ID
					success(res) {
						console.log('订阅成功', res);
						// 用户同意订阅后，可调用后端接口发送消息
						// 例如：uni.request({ url: 'https://xxxxxx.com/sendSubscribeMessage', method: 'POST' })
					},
					fail(err) {
						console.log('订阅失败', err);
					}
				});
			},
			goToCityWorkCenter(){
				uni.showToast({
					title: '城市运营中心开发中,敬请期待...',
					icon: 'none'
				})
			}
			

		}
	};
</script>

<style lang="scss" scoped>
	.tui-header-box {
		width: 100%;
		padding: 0 30rpx 0 20rpx;
		box-sizing: border-box;
		position: fixed;
		top: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		height: 32px;
		transform: translateZ(0);
		z-index: 9999;
	}

	/* #ifndef MP */
	.tui-header-icon {
		min-width: 120rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	/* #endif */
	.tui-icon-box {
		position: relative;
	}

	.tui-badge {
		position: absolute;
		font-size: 24rpx;
		height: 32rpx;
		min-width: 20rpx;
		padding: 0 6rpx;
		border-radius: 40rpx;
		right: 10rpx;
		top: -5rpx;
		transform: scale(0.8) translateX(60%);
		transform-origin: center center;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 10;
	}

	.tui-badge-red {
		background: #f74d54;
		color: #fff;
	}

	.tui-mybg-box {		
		margin: 0 30rpx;
		border-radius: 12rpx;
		position: relative;
		margin-top: -350rpx;
		z-index: 2;
		padding:30rpx;
		background: white;
	}

	.tui-header-center {
		width: 100%;
		height: 128rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-avatar {
		flex-shrink: 0;
		width: 92rpx;
		height: 92rpx;
		display: block;
		border-radius: 50%;
		cursor: pointer;
		transition: transform 0.2s ease;
	}
	
	.tui-avatar:active {
		transform: scale(0.95);
	}

	.tui-info {		
		padding-left: 30rpx;
		display: flex;
		flex: 1;
	}

	.tui-login {
		width: 60%;
		padding-left: 30rpx;
		font-size: 32rpx;
		line-height: 32rpx;
		display: flex;
		align-items: center;
		color: #666;
	}

	.tui-nickname {
		font-size: 36rpx;
		color: #333333;
		font-weight: 500;
		display: flex;
		align-items: center;
	}
	.header-btn-group {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		margin-left: auto;
	}
	.header-btn-item {
		background: none; /* 去掉背景 */
		color: inherit; /* 文字颜色继承父元素 */
		border: none; /* 去掉边框 */
		outline: none;
		padding: 0; /* 去掉内边距 */
		margin: 0; /* 去掉外边距 */
		font: inherit; /* 字体样式继承父元素 */
		cursor: pointer; /* 鼠标样式为指针 */
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-left: 44rpx;
		font-size: 24rpx;
		.header-item-img {
			width: 44rpx;
			height: 44rpx;
			display: block;
		}
	}
	.tui-header-btm {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 50rpx;
		color: #666;
	}
	.tui-btm-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: relative; /* 这里是关键 */
	}
	.tui-btm-item:not(:last-child)::before{
		content: '';
		width: 1rpx; /* 边框宽度 */
		height: 50rpx; /* 边框长度 */
		position: absolute;
		right: 0; /* 设置为右边框 */
		background-color: #EFEFEF; /* 边框的颜色 */
	}
	.tui-btm-num {
		font-size: 36rpx;
		font-weight: 600;
		position: relative;
		color: #333;
	}

	.tui-swiper {		
		width: 100%;
		height: 160rpx;
		image{
			width: 100%;
			height: 100%;
		}
	}

	.tui-btm-text {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}

	.tui-content-box {
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		z-index: 3;
		margin-top: 40rpx;
	}
	.tui-box {
		width: 100%;
		background: #fff;
		box-shadow: 0 3rpx 20rpx rgba(183, 183, 183, 0.1);
		border-radius: 12rpx;
		overflow: hidden;
		padding: 30rpx 0;
	}
	.tui-cell-header {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		
	}
	.tui-cell-title {
		font-size: 30rpx;
		line-height: 30rpx;
		color: #333;
		padding-left: 28rpx;
	}
	.tui-cell-sub {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		font-weight: 400;
		color: #999;
		padding-right: 28rpx;
	}

	.tui-order-list {
		box-sizing: border-box;
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		margin-top: 30rpx;
	}

	.tui-order-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.tui-order-text,
	.tui-tool-text {
		font-size: 26rpx;
		font-weight: 400;
		color: #666;
		margin-top: 10rpx;
	}

	.tui-order-icon {
		width: 56rpx;
		height: 56rpx;
		display: block;
	}
	.tui-tool-box {
		margin-top: 30rpx;
	}
	.tui-flex-wrap {
		flex-wrap: wrap;
		height: auto;
	}
	.tui-tool-item {
		width: 25%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}
	.tui-tool-icon {
		width: 64rpx;
		height: 64rpx;
		display: block;
	}

	.contact-btn{
		background: none; /* 去掉背景 */
		color: inherit; /* 文字颜色继承父元素 */
		border: none; /* 去掉边框 */
		outline: none;
		padding: 0; /* 去掉内边距 */
		margin: 0; /* 去掉外边距 */
		font: inherit; /* 字体样式继承父元素 */
		cursor: pointer; /* 鼠标样式为指针 */
	}
	button::after{
		border: none;
	}
	
	.tui-header-bg {
		width: 100%;
		height: 532rpx; 
		position: relative;
		background: url('https://admin.cqycgyl.com/uploads/20250916/fd7b6250ab9ba3832cf7f45abc78251e.png') no-repeat top center;
		background-size: 100%;		
	}
	.tui-header-img {
		width: 100%;
		height: 440rpx;
		display: block;
	}
	.tui-header-title {
		width: 100%;
		text-align: center;
		color: #fff;
		height: 44px;
		line-height: 44px;
		padding-top: 20rpx;
		/* position: absolute; */
	}
</style>
